// *******************************************************************************************************
// # リスト検索画面
//   ・折りたたみ
//   ・モーダル 位置
//   ・モーダル 設定
// *******************************************************************************************************

(function(win, doc) {

	/* 折りたたみ
	----------------------------------- */
	function expandMenu() {
		var $explist = $('.list_expand');
		var $expTitle = $('.ttl_expand .ttl_inner');
		//初期状態：非表示
		$explist.show();
		//ボタン
		$expTitle.on('click',function(){
			$(this).toggleClass('close');
			$(this).parent().next().toggle();
		});
	}

	/* モーダル 位置
	----------------------------------- */
	function modalPosition() {
		var timer = null;
		$('a.modal_link').on('click', function(e){
			var _id = $(this).attr('href'),
				_st = $(window).scrollTop(),
				_wh = $(window).height(),
				_ch = $(_id).height();
			if (_wh > _ch) {
				$(_id).css({ 'top': _st + (_wh-_ch)*0.5 });
			} else {
				$(_id).css({ 'top': _st + 20 });
			};
		});
	};

	/* モーダル 設定
	----------------------------------- */
	function modalSetting() {
		$('a.modal_inner_close').on('click', function(e){
			$('.conditions_modal1').hide();
		});

		$('#delete00_conf a.btn_cancel').on('click', function(e){
			$('#lean_overlay').trigger('click');
		});
	};
	
	function bindData(){
		var storage = localStorage;
		
		//条件１
		var cons1Name = storage.getItem('condition1Text');		
		if(cons1Name == undefined || cons1Name == '') return;
		cons1Name = JSON.parse(cons1Name);
		//console.dir(cons1Name);
		assignData(cons1Name, '1');		
		
		//条件２
		var cons2Name = storage.getItem('condition2Text');		
		if(cons2Name == undefined || cons2Name == '') return;
		cons2Name = JSON.parse(cons2Name);
		assignData(cons2Name, '2');
		
		//条件3
		var cons3Name = storage.getItem('condition3Text');		
		if(cons3Name == undefined || cons3Name == '') return;
		cons3Name = JSON.parse(cons3Name);
		assignData(cons3Name, '3');
		
		
	}
	
	function assignData(data, index){
		//メインデータ
		var mainVal = data[0].mainData;		
		for(var i = 0 ; i < mainVal.length ; i++){
			
			var splitVals = mainVal[i].split(' ');			
			if(splitVals.length <= 2){
				$('#ulMainData' + index).append('<li>' + mainVal[i] + '</li>');
			}
			else {
				var val1 = splitVals[0] + " " + splitVals[1];
				var val2 = splitVals[2];				
				$('#ulMainData' + index).append('<li>' + val1 + '<br><span>（' + val2 +  '）</span></li>');
			}
		}				
		//賃料
		$('#tinryo' + index).text(data[1].tinryo);
		//間取り 
		$('#madori' + index).text(data[2].madori);
		//専有面積
		$('#senyu' + index).text(data[3].senyu);
		//物件種別
		$('#syubetu' + index).text(data[4].syubetu);
		//築年数
		$('#build' + index).text(data[5].build);
		//駅徒歩
		$('#ekitoho' + index).text(data[6].ekitoho);
		//その他
		$('#other' + index).text(data[7].other);
		
		//保存日付
		$('#savetime' + index).text(data[8].savetime);
		
		var hour = data[8].savetime.split(' ')[1].split(':')[0];
		if(hour.indexOf('0') == 0) hour = hour.substring(1);
		hour = parseInt(hour);		
		if(hour >= 4 && hour <= 10){
			$('#saveimage' + index).addClass('icn_bear01');
		}
		else if(hour >= 11 && hour <= 20){
			$('#saveimage' + index).addClass('icn_bear03');
		}
		else{
			$('#saveimage' + index).addClass('icn_bear02');
		}
	}
	
	function submit(action, method, values) {
 	   var form = $('<form/>', {
	        action: action,
	        method: method
	    });
	    $.each(values, function() {
	        form.append($('<input/>', {
	            type: 'hidden',
	            name: this.name,
	            value: this.value
	        }));    
	    });
	    form.appendTo('body').submit();
	}
	
	//検索
	$('.mod_btn_all_pink').on('click', function(){
		var cons = $.cookie('condition1');
		if($(this).attr('id') == 'cons2') cons = $.cookie('condition2');
		else if($(this).attr('id') == 'cons3') cons = $.cookie('condition3');
		var vals = cons.split('|');
		var items=[];
		for(var i = 0 ; i < vals.length; i++){
			var s = vals[i].split(':')
			items.push({name:s[0], value:s[1]});
		}
		//console.dir(items);
		items.push({name:'page', value:'1'});
		submit('/sp/rstobi/rstobi', 'POST', items);
	});

	//条件すべて削除
	$('#lnkDelAll').on('click', function(){
		CONS.removeConditionAll();
	});
	
	//条件1個削除
	$('a[id^=lnkDelSelf]').on('click', function(){
		var id = $(this).attr('id');
		CONS.removeCondition(id.replace('lnkDelSelf', ''));
	});
	
	//条件問い合わせ
	$('a[id^=lnkinquiry]').on('click', function(e){		
		e.preventDefault();		
		var index = $(this).attr('id').replace('lnkinquiry', '');
		var cons = $.cookie('condition' + index);
		var consName = JSON.parse(localStorage.getItem('condition' + index + 'Text'));
		var mainData = consName[0].mainData;
		
		var items = [];
		var codes = [];
		
		//console.dir(consName);
		var vals = cons.split('|'); //クッキー分析
		
		var keyHash = {};
		for(var i = 0 ; i<vals.length; i++){
			var rets = vals[i].split(':'); //クッキー「キー、値」
			keyHash[rets[0]] = rets[1];
		}
		
		var square = ['0','1','15','20','25','30','35','40','45','50','60','70','80','90','100','99999'];
		var eki = ['0','1','5','7','10','15'];
		var madoris = ['10', '11', '12', '14', '21', '22', '24', '31', '32', '34', '41', '42', '44', '51'];
		var builts = ['0', '1', '3', '5', '10'];
		var ko1 = [136,20,21,22,23,24];
		var ko2 = [94,116,117,118,119,120,121,122,125,126];
		var ko3 = [9,10,11,12,14,15,18,19,132,134,133,135];
		var ko4 = [31,32,36,93,95,130,131];
		var ko5 = [53,54,55,56,97,98,103,104,111];
		var ko6 = [33,34,75,107,123,124];
		var ko7 = [4,5,8,46,61,62,66,73,77,87,101,112,127,128,129];
		var ko8 = [59,60,67,78,79,80,114];
		var ko9 = [44,45,63,65,81,83,86,113];
		var ko10 = [68,70,90,91,115];
		var ko11 = [7,38,39];
		
		//name
		var search_type = keyHash['search_type'];
		if(search_type == 'shop') items.push({name:'name',value:mainData[0]});
		else if(search_type == 'area'){
			var names = [];
			for(var i = 0 ; i < mainData.length ; i++){
				var ret = mainData[i].replace(new RegExp('　', 'g'), ' ');
				var vals = ret.split(' ');
				
				if(vals.length <= 2) names.push(ret.replace(' ', '　'));
				else names.push(vals[0] + '　' + vals[1] + ';' + vals[2]);
			}
			items.push({name:'name',value:names.join('/')});
		}		
		else if(search_type == 'line'){
			var names = [];
			for(var i = 0 ; i < mainData.length ; i++){
				var ret = mainData[i].replace(new RegExp('　', 'g'), ' ');
				var vals = ret.split(' ');
				
				if(vals.length <= 2) names.push(ret.replace(' ', '　'));
				else names.push(vals[0] + '　' + vals[1] + ';' + vals[2]);
			}
			items.push({name:'name',value:names.join('/')});
		}
				
		//コード
		for(var name in keyHash){
			var value = keyHash[name];
			if(name == 'search_type'){
				if(value == 'area') items.push({name:'kind', value:'1'});
				else if(value == 'line') items.push({name:'kind', value:'2'});
				else if(value == 'shop') items.push({name:'kind', value:'3'});
			}
			else if(name.indexOf('target') >= 0){ //コード
				codes.push(value.split('-')[1]);
			}
			else if(name.indexOf('tinryo') >= 0 && name != 'tinryo_jouken2'){ //賃料
				if(value == 9999999) value = 9990000;
				items.push({name:name, value:value/10000});
			}	
			else if(name.indexOf('menseki') >= 0 || name.indexOf('senyu') >= 0){ //面積
				var index = $.inArray(keyHash[name], square);
				if(name == 'senyu1'){
					items.push({name:'senyu1', value: index});
				}
				else if(name == 'senyu2'){
					items.push({name:'senyu2', value: index});
				}
			}			
		}	
		
		items.push({name:'code', value:codes.join(',')});
		
		if(keyHash['tinryo_jouken2'] == 'on') items.push({name:'tinryo_jouken2', value: '1'});
		else items.push({name:'tinryo_jouken2', value: '0'});		
		
		if(keyHash['tyusyajyo_komi'] == 'on') items.push({name:'tyusyajyo_komi', value: '1'});
		else items.push({name:'tyusyajyo_komi', value: '0'});
		
		
		if(keyHash['shikikin_nashi'] == 'on') items.push({name:'shikikin_nashi', value: '1'});
		else items.push({name:'shikikin_nashi', value: '0'});
		
		if(keyHash['reikin_nashi'] == 'on') items.push({name:'reikin_nashi', value: '1'});
		else items.push({name:'reikin_nashi', value: '0'});
		
		if(keyHash['freerent'] == 'on') items.push({name:'freerent', value: '1'});
		else items.push({name:'freerent', value: '0'});
		
		if(keyHash['heyawari'] == 'on') items.push({name:'heyawari', value: '1'});
		else items.push({name:'heyawari', value: '0'});
		
		if(keyHash['syubetu1'] == 'on') items.push({name:'syubetu1', value: '1'});
		else items.push({name:'syubetu1', value: '0'});
		
		if(keyHash['syubetu2'] == 'on') items.push({name:'syubetu2', value: '1'});
		else items.push({name:'syubetu2', value: '0'});
		
		if(keyHash['syubetu3'] == 'on') items.push({name:'syubetu3', value: '1'});
		else items.push({name:'syubetu3', value: '0'});
		
		if(keyHash['syubetu4'] == 'on') items.push({name:'syubetu4', value: '1'});
		else items.push({name:'syubetu4', value: '0'});
		
		if(keyHash['syubetu5'] == 'on') items.push({name:'syubetu5', value: '1'});
		else items.push({name:'syubetu5', value: '0'});
		
		items.push({name:'ekitoho', value:$.inArray(keyHash['ekitoho'],eki)});
		if(keyHash['bas_riyou'] == 'on') items.push({name:'bas_riyou', value: '1'});
		else items.push({name:'bas_riyou', value: '0'});
		
		//間取り
		for(var i = 0 ; i < madoris.length ; i++){
			if(keyHash['madori' + madoris[i]] == 'on') items.push({name:'madori' + madoris[i], value: '1'});
			else items.push({name:'madori' + madoris[i], value: '0'});
		}
		items.push({name:'madoriGroupCheck', value:'0'});
		
		//builts
		if(keyHash['built'] != undefined){
			items.push({name:'built', value:$.inArray(keyHash['built'], builts)});
		}else{
			items.push({name:'built', value:'0'});
		}
		
		//人気こだわり
		for(var i = 1; i <= 9 ; i++){
			var ninki = 'kodawari_0' + String(i);
			if(keyHash[ninki] == 'on') items.push({name:ninki, value: '1'});
			else items.push({name:ninki, value: '0'});
		}
		
		//間取りGroup
		var hasOne = false;
		for(var i = 0 ; i < ko1.length ; i++){
			var maName = 'kodawari' + String(ko1[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_01', value: '1'});					
				}
				items.push({name:maName,value:'1'});
			}
			else {
				items.push({name:maName,value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_01', value: '0'});
		
		
		hasOne = false;
		for(var i = 0 ; i < ko2.length ; i++){
			var maName = 'kodawari' + String(ko2[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_02', value: '1'});					
				}
				items.push({name:maName,value:'1'});
			}
			else {
				items.push({name:maName,value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_02', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko3.length ; i++){
			var maName = 'kodawari' + String(ko3[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_03', value: '1'});					
				}
				items.push({name:maName,value:'1'});
			}
			else {
				items.push({name:maName,value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_03', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko4.length ; i++){
			var maName = 'kodawari' + String(ko4[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_04', value: '1'});					
				}
				items.push({name:maName,value:'1'});
			}
			else {
				items.push({name:maName,value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_04', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko5.length ; i++){
			var maName = 'kodawari' + String(ko5[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_05', value: '1'});					
				}
				items.push({name:maName,value:'1'});
			}
			else {
				items.push({name:maName,value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_05', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko6.length ; i++){
			var maName = 'kodawari' + String(ko6[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_06', value: '1'});					
				}
				items.push({name:maName,value:'1'});
			}
			else {
				items.push({name:maName,value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_06', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko7.length ; i++){
			var maName = 'kodawari' + ko7[i];
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_07', value: '1'});					
				}
				items.push({name:maName,value:'1'});
			}
			else {
				items.push({name:maName,value:'0'});
			}
			
		}
		if(!hasOne)items.push({name:'kodawariType_07', value: '0'});

		
		hasOne = false;
		for(var i = 0 ; i < ko8.length ; i++){
			var maName = 'kodawari' + String(ko8[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_08', value: '1'});					
				}
				items.push({name:maName,value:'1'});
			}
			else {
				items.push({name:maName,value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_08', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko9.length ; i++){
			var maName = 'kodawari' + String(ko9[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_09', value: '1'});					
				}
				items.push({name:maName,value:'1'});
			}
			else {
				items.push({name:maName,value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_09', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko10.length ; i++){
			var maName = 'kodawari' + String(ko10[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_10', value: '1'});					
				}
				items.push({name:maName,value:'1'});
			}
			else {
				items.push({name:maName,value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_10', value: '0'});
		
		hasOne = false;
		for(var i = 0 ; i < ko11.length ; i++){
			var maName = 'kodawari' + String(ko11[i]);
			if(keyHash[maName] == 'on'){
				if(!hasOne){
					hasOne = true;
					items.push({name:'kodawariType_11', value: '1'});					
				}
				items.push({name:maName,value:'1'});
			}
			else {
				items.push({name:maName,value:'0'});
			}
		}
		if(!hasOne)items.push({name:'kodawariType_11', value: '0'});
		
		//キャンペイン
		var campaign = [];		
		var cpList = consName[consName.length - 2].other;
		if(cpList.indexOf('バルセロナ部屋') >= 0) campaign.push('バルセロナ部屋');
		if(cpList.indexOf('ポンタ部屋') >= 0) campaign.push('ポンタ部屋');
		if(cpList.indexOf('友の会部屋') >= 0) campaign.push('友の会部屋');
		if(cpList.indexOf('家電部屋特集') >= 0) campaign.push('家電部屋特集');
		items.push({name:'campaignName', value:campaign.join('/')});
		//キャンペイン
		
		//console.dir(items);		
		submit($(this).attr('href'), 'POST', items);
		
	});
	
	//自動リロー初回
	if(document.URL.indexOf("#")==-1){
        // Set the URL to whatever it was plus "#".
        //url = document.URL+"#";
        //location = "#";

        //Reload the page
        //location.reload(true);
    }
	
	/* 実行
	----------------------------------- */
	$(function() {
		expandMenu();
		modalPosition();
		modalSetting();
		bindData();
	});

})(this, document);